package com.fly.modules.sys.controller;

import com.fly.modules.sys.entity.Area;
import com.fly.modules.sys.service.AreaService;
import com.fly.modules.sys.utils.DictUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by swl on 2017/4/30 0030.
 * 区域Controller
 */
@Controller
@RequestMapping("/sys/area")
public class AreaController {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    AreaService areaService;

    @RequestMapping(value = "/list")
    public ModelAndView list() {
        String value = DictUtils.getDictLabel("2" , "sys_area_type" ,"国家");
        System.out.println("value:" + value);
        ModelAndView mv = new ModelAndView("/modules/sys/areaList");
        List<Area> areaList = areaService.findAll();
        mv.addObject("areaList", areaList);
        return mv;
    }

    @RequestMapping(value = "/form")
    public ModelAndView form() {
        ModelAndView mv = new ModelAndView("modules/sys/areaForm");
        return mv;
    }

    @RequestMapping(value = "/treeData")
    public  @ResponseBody
    List<Map<String, Object>> treeData(){
        logger.info("treeData");
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<Area> list = areaService.findAll();
        for (int i=0; i<list.size(); i++){
            Area e = list.get(i);
            Map<String, Object> map = new HashMap<String, Object>();
            logger.info(e.getName());
            map.put("id", e.getId());
            map.put("pId", e.getParentId());
            map.put("name", e.getName());
            mapList.add(map);

        }
        return mapList;
    }
}
